﻿using DbFactory;
using PlaneClean.Models;
using PlaneClean.ViewModels.PlayClean;
using PlaneCleanEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using System.Web;

namespace PlaneClean.Service
{
    public class CleanWorkService
    {
        /// <summary>
        /// 返回清洗参数总计
        /// </summary>
        public static RainParamsResopnse rp;
        /// <summary>
        /// 清洗记录id
        /// </summary>
        public static int cr_id;
        /// <summary>
        /// 最后请求获取清洗参数时间
        /// </summary>
        public static DateTime lastGetRainParamsTime;
        /// <summary>
        /// 当前请求次数
        /// </summary>
        public static int currentCount = 0;
        /// <summary>
        /// 计时器
        /// </summary>
        public static Timer tm;
        /// <summary>
        /// 通知PLC开始清洗 并开启timer
        /// </summary>
        /// <returns></returns>
        public static void TimerStart()
        {
            //写到serivce中
            currentCount = 0;
            rp = new RainParamsResopnse();
            lastGetRainParamsTime = DateTime.Now;
            tm = new Timer(1000);
            //tm.AutoReset = true;
            //tm.Elapsed += delegate (object sender, ElapsedEventArgs e)
            //{

            //    TimeSpan ts1 = new TimeSpan(lastGetRainParamsTime.Ticks);
            //    TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
            //    TimeSpan ts = ts2.Subtract(ts1).Duration();
            //    if (ts.Seconds > 10)
            //    {
            //        TimerEnd();
            //    }
            //};
            tm.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            //设置时间间隔为2秒（2000毫秒），覆盖构造函数设置的间隔
            tm.Interval = 10000;
            //设置是执行一次（false）还是一直执行(true)，默认为true
            tm.AutoReset = true;
            //开始计时
            tm.Enabled = true;
        }
        /// <summary>
        /// 通知PLC结束清洗
        /// </summary>
        /// <returns></returns>
        public static int TimerEnd()
        {

            tm.Enabled = false;
            tm.Dispose();

            //置空rp
            rp = new RainParamsResopnse();
            return currentCount;
        }
        //指定Timer触发的事件
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {

            TimeSpan ts1 = new TimeSpan(lastGetRainParamsTime.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan ts = ts2.Subtract(ts1).Duration();
            if (ts.Seconds >= 10)
            {
                tm.Enabled = false;
                tm.Dispose();
                TimerEnd();
                t_clean_record cr = dbFactory.Db.Queryable<t_clean_record>().First(a => a.cr_id == CleanWorkService.cr_id);
                if (cr == null)
                {
                    throw new Exception("清洗记录id=" + CleanWorkService.cr_id + "的记录不存在");
                }
                cr.cr_end_time = DateTime.Now;
                cr.memo = "清洗工作异常关闭";
                dbFactory.Db.Updateable(cr).ExecuteCommand();
            }
        }

    }
}